﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MediaManager
{
    public class Global
    {
        private static Global _instance = null;
        public static Global Instance
        {
            get 
            {
                if (null == _instance)
                {
                    _instance = new Global();
                }
                return _instance;
            }
        }
        private MDIManage _mdiManage = null;
        private string _userName = string.Empty;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        public MDIManage MdiManage
        {
            get { return _mdiManage; }
            set { _mdiManage = value; }
        }
        /// <summary>
        /// 取得最大可用索引
        /// </summary>
        /// <param name="cbcb"></param>
        /// <returns></returns>
        public int GetCheckComboboxCount(PresentationControls.CheckBoxComboBox cbcb)
        {
            int index = 0;
            int count = cbcb.CheckBoxItems.Count;
            for (int i = count - 1; i >= 1; i--)
            {
                if (cbcb.CheckBoxItems[i].Checked)
                {
                    index = i + 1;
                    break;
                }
            }
            return index;
        }
        public void ShowFormID(System.Windows.Forms.DataGridView dataGridView1, List<BaseTable> listBaseTable)
        {
            try
            {
                if (dataGridView1.DataSource == null)
                {
                    GlobalBase.Global.Instance.ShowMessage("请先查询数据！");
                    return;
                }
                int j = dataGridView1.CurrentRow.Index;
                if (j < 0)
                {
                    GlobalBase.Global.Instance.ShowMessage("请先选中要查看的数据行！");
                    return;
                }
                if (null != listBaseTable && listBaseTable.Count > 0)
                {
                    BaseTable baseTable = listBaseTable.ElementAt(j);
                    string s = "确实查看编号为：{0}，姓名为：{1}的这条数据吗？";
                    s = string.Format(s, baseTable.编号, baseTable.姓名);
                    if (MessageBox.Show(s, "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if ((null != MDIManage._manage) &&(!MDIManage._manage.IsDisposed))
                        {
                            MDIManage._manage.Show();
                            MDIManage._manage.Focus();

                        }
                        else
                        {
                            MDIManage._manage = new Manage(_userName);
                            MDIManage._manage.MdiParent = _mdiManage;
                            MDIManage._manage.Show();
                            MDIManage._manage.Focus();
                        }
                        MDIManage._manage.UpdateMediaData(baseTable.编号);
                    }
                }
                else
                {
                    throw new Exception("提取数据错误！");
                }

            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
                GlobalBase.Global.Instance.ShowMessage(ex.Message);
            }
        }
        public void ShowFormID(System.Windows.Forms.DataGridView dataGridView1, List<BaseTable> listBaseTable, Manage manage)
        {
            ShowFormID(dataGridView1, listBaseTable);
        }
    }
}
